package com.qf.hello.spring;

import java.io.InputStream;
import java.util.Properties;

public class MyFactory {

    private Properties properties;

    public MyFactory() {
        try {
            //读取配置文件
            InputStream inputStream = MyFactory.class.getResourceAsStream("/bean.properties");
            //把配置文件信息写入到一个properties对象中key,value
            properties = new Properties();
            properties.load(inputStream);
            System.out.println("inputStream = " + inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //提供一个根据传入的名字获取对象实例的方法
    public Object getBean(String name) throws Exception {
        //classPath类的完整名字
        String classPath = (String)properties.get(name);
        Class<?> cls = Class.forName(classPath);
        // newInstance 获取类的实例 new 对象();
        return cls.newInstance();
    }
}
